﻿Imports Microsoft.Office.Interop.Word
Imports System.Windows.Forms
Imports Microsoft.Office.Tools.Ribbon
Imports System.Runtime.Remoting
Imports Microsoft.VisualBasic.Devices
Imports System.IO
Imports Newtonsoft.Json
Imports System.Web.Script.Serialization
Imports Microsoft.VisualBasic.ApplicationServices
Imports System.Diagnostics
Imports Microsoft.Office.Tools
Imports System.Reflection
Imports Microsoft.Office.Core
Imports System.Drawing
Imports stdole
Imports System.Runtime.CompilerServices

Public Class Ribbon1
    Public oWord As Microsoft.Office.Interop.Word.Application
    Public obj As Object
    Public myConfigPath As String
    Public myTemplatePath As String
    Public appFilePath = Environment.GetFolderPath（Environment.SpecialFolder.ApplicationData） + "\idoc"
    Public newMenuBar As Office.CommandBarPopup
    Public btn保存页码配置 As Office.CommandBarButton
    Public btn保存公文标题配置 As Office.CommandBarButton
    Public btn保存二级标题配置 As Office.CommandBarButton
    Public btn保存三级标题配置 As Office.CommandBarButton
    Public btn保存正文配置 As Office.CommandBarButton
    Public btn保存小行间距配置 As Office.CommandBarButton
    Public btn保存中行间距配置 As Office.CommandBarButton
    Public btn保存大行间距配置 As Office.CommandBarButton
    Public btn保存页面配置 As Office.CommandBarButton
    Public Sub 获取配置（）

        Using sr As StreamReader = New StreamReader(myConfigPath)
            Dim jsonText = sr.ReadToEnd()
            Dim JSON As New JavaScriptSerializer()
            obj = JSON.Deserialize(Of Object)(jsonText)
        End Using
    End Sub
    Public Sub 保存配置（）
        Dim jsonConents = JsonConvert.SerializeObject(Globals.Ribbons.Ribbon1.obj, Formatting.Indented)
        Dim fs = New FileStream(myConfigPath, FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite)
        fs.Seek(0, SeekOrigin.Begin)
        fs.SetLength(0)
        Dim sw = New StreamWriter(fs, Encoding.UTF8)
        sw.WriteLine(jsonConents)
        sw.Close()
        fs.Close()
    End Sub

    Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
        myConfigPath = appFilePath & "\config\config.json"
        myTemplatePath = appFilePath & "\config\mb.dotx"
        '读取config配置
        oWord = Globals.ThisAddIn.Application
        oWord.AddIns.Add(myTemplatePath, True) '加载自定义模板
        获取配置()

        newMenuBar = oWord.CommandBars("Text").Controls.Add(Office.MsoControlType.msoControlPopup）
        With newMenuBar
            .BeginGroup = True
            .Tag = "公文配置"
            .Caption = "公文配置"
        End With
        btn保存页码配置 = newMenuBar.Controls.Add(Office.MsoControlType.msoControlButton)
        AddHandler btn保存页码配置.Click, AddressOf 保存页码配置
        With btn保存页码配置
            .Caption = "保存页码配置"
            .Picture = CovertImage.Convert(My.Resources.井)
            .Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption
        End With



        btn保存公文标题配置 = newMenuBar.Controls.Add(Office.MsoControlType.msoControlButton)
        With btn保存公文标题配置
            .BeginGroup = True
            .Tag = "公文标题配置"
            .Picture = CovertImage.Convert(My.Resources.H1)
            .Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption
            .Caption = "公文标题配置"
        End With
        AddHandler btn保存公文标题配置.Click, AddressOf 保存公文标题配置

        btn保存二级标题配置 = newMenuBar.Controls.Add(Office.MsoControlType.msoControlButton)
        With btn保存二级标题配置
            .Tag = "二级标题配置"
            .Caption = "二级标题配置"
            .Picture = CovertImage.Convert(My.Resources.H2)
            .Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption
        End With
        AddHandler btn保存二级标题配置.Click, AddressOf 保存二级标题配置

        btn保存三级标题配置 = newMenuBar.Controls.Add(Office.MsoControlType.msoControlButton)
        With btn保存三级标题配置
            .Tag = "三级标题配置"
            .Caption = "三级标题配置"
            .Picture = CovertImage.Convert(My.Resources.H3)
            .Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption
        End With
        AddHandler btn保存三级标题配置.Click, AddressOf 保存三级标题配置

        btn保存正文配置 = newMenuBar.Controls.Add(Office.MsoControlType.msoControlButton)
        With btn保存正文配置
            .Tag = "正文配置"
            .Caption = "正文配置"
            .Picture = CovertImage.Convert(My.Resources.正文)
            .Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption
        End With
        AddHandler btn保存正文配置.Click, AddressOf 保存正文配置

        btn保存小行间距配置 = newMenuBar.Controls.Add(Office.MsoControlType.msoControlButton)
        With btn保存小行间距配置
            .BeginGroup = True
            .Tag = "小行间距配置"
            .Caption = "小行间距配置"
            .Picture = CovertImage.Convert(My.Resources.S)
            .Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption
        End With
        AddHandler btn保存小行间距配置.Click, AddressOf 保存小行间距配置

        btn保存中行间距配置 = newMenuBar.Controls.Add(Office.MsoControlType.msoControlButton)
        With btn保存中行间距配置
            .Tag = "中行间距配置"
            .Caption = "中行间距配置"
            .Picture = CovertImage.Convert(My.Resources.M)
            .Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption
        End With
        AddHandler btn保存中行间距配置.Click, AddressOf 保存中行间距配置

        btn保存大行间距配置 = newMenuBar.Controls.Add(Office.MsoControlType.msoControlButton)
        With btn保存大行间距配置
            .Tag = "大行间距配置"
            .Caption = "大行间距配置"
            .Picture = CovertImage.Convert(My.Resources.L)
            .Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption
        End With
        AddHandler btn保存大行间距配置.Click, AddressOf 保存大行间距配置

        btn保存页面配置 = newMenuBar.Controls.Add(Office.MsoControlType.msoControlButton)
        With btn保存页面配置
            .BeginGroup = True
            .Tag = "保存页面配置"
            .Caption = "保存页面配置"
            .Picture = CovertImage.Convert(My.Resources.page)
            .Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption
        End With
        AddHandler btn保存页面配置.Click, AddressOf 保存页面配置


    End Sub




    Private Sub 插入页码_Click(sender As Object, e As RibbonControlEventArgs) Handles 插入页码.Click
        'oWord.ActiveDocument.Range.InsertAfter("haha ")

        Dim objTemplate As Template
        Dim objBB As BuildingBlock
        With oWord.ActiveWindow.ActivePane.View
            .SeekView = WdSeekView.wdSeekCurrentPageFooter
            objTemplate = oWord.Templates(1)
            objBB = objTemplate.BuildingBlockEntries.Item("页码")
            objBB.Insert(oWord.Selection.Range)
            With oWord.Selection
                .ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter '页码的位置居中
                With oWord.ActiveDocument.Sections(1).Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary)
                    .PageNumbers.NumberStyle = obj.item("页码"）.item("style"） '页码风格
                End With
            End With
            .SeekView = WdSeekView.wdSeekMainDocument '退出页码的编辑（即关闭页眉和页脚工具栏）

        End With
    End Sub
    Public Sub 设置格式（type As String）
        Dim myParagraphFormat = oWord.Selection.ParagraphFormat.Duplicate
        With myParagraphFormat
            .AddSpaceBetweenFarEastAndAlpha = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("AddSpaceBetweenFarEastAndAlpha")
            .AddSpaceBetweenFarEastAndDigit = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("AddSpaceBetweenFarEastAndDigit")
            .Alignment = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("Alignment")
            .AutoAdjustRightIndent = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("AutoAdjustRightIndent")
            .BaseLineAlignment = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("BaseLineAlignment")
            .CharacterUnitFirstLineIndent = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("CharacterUnitFirstLineIndent")
            .CharacterUnitLeftIndent = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("CharacterUnitLeftIndent")
            .CharacterUnitRightIndent = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("CharacterUnitRightIndent")
            .CollapsedByDefault = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("CollapsedByDefault")
            .DisableLineHeightGrid = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("DisableLineHeightGrid")
            .FarEastLineBreakControl = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("FarEastLineBreakControl")
            .FirstLineIndent = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("FirstLineIndent")
            .HalfWidthPunctuationOnTopOfLine = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("HalfWidthPunctuationOnTopOfLine")
            .HangingPunctuation = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("HangingPunctuation")
            .Hyphenation = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("Hyphenation")
            .KeepTogether = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("KeepTogether")
            .KeepWithNext = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("KeepWithNext")
            .LeftIndent = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("LeftIndent")
            .LineSpacing = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("LineSpacing")
            .LineSpacingRule = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("LineSpacingRule")
            .LineUnitAfter = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("LineUnitAfter")
            .LineUnitBefore = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("LineUnitBefore")
            .MirrorIndents = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("MirrorIndents")
            .NoLineNumber = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("NoLineNumber")
            .OutlineLevel = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("OutlineLevel")
            .PageBreakBefore = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("PageBreakBefore")
            .ReadingOrder = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("ReadingOrder")
            .RightIndent = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("RightIndent")
            .Shading.BackgroundPatternColor = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("Shading").item（"BackgroundPatternColor")
            .Shading.BackgroundPatternColorIndex = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("Shading").item（"BackgroundPatternColorIndex")
            .Shading.ForegroundPatternColor = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("Shading").item（"ForegroundPatternColor")
            .Shading.ForegroundPatternColorIndex = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("Shading").item（"ForegroundPatternColorIndex")
            .Shading.Texture = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("Shading").item（"Texture")
            .SpaceAfter = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("SpaceAfter")
            .SpaceAfterAuto = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("SpaceAfterAuto")
            .SpaceBefore = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("SpaceBefore")
            .SpaceBeforeAuto = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("SpaceBeforeAuto")
            .TextboxTightWrap = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("TextboxTightWrap")
            .WidowControl = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("WidowControl")
            .WordWrap = Globals.Ribbons.Ribbon1.obj.item(type).item("段落").item("WordWrap")
        End With
        oWord.Selection.ParagraphFormat = myParagraphFormat

        Dim myFontFormat = oWord.Selection.Font.Duplicate
        With myFontFormat
            .AllCaps = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("AllCaps")
            .Bold = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Bold")
            .BoldBi = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("BoldBi")
            .ColorIndex = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("ColorIndex")
            .ColorIndexBi = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("ColorIndexBi")
            .ContextualAlternates = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("ContextualAlternates")
            .DiacriticColor = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("DiacriticColor")
            .DisableCharacterSpaceGrid = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("DisableCharacterSpaceGrid")
            .DoubleStrikeThrough = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("DoubleStrikeThrough")
            .Emboss = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Emboss")
            .EmphasisMark = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("EmphasisMark")
            .Engrave = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Engrave")
            .Hidden = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Hidden")
            .Italic = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Italic")
            .ItalicBi = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("ItalicBi")
            .Kerning = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Kerning")
            .Ligatures = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Ligatures")
            .Name = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Name")
            .NameAscii = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("NameAscii")
            .NameFarEast = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("NameFarEast")
            .NameOther = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("NameOther")
            .NumberForm = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("NumberForm")
            .NumberSpacing = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("NumberSpacing")
            .Outline = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Outline")
            .Position = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Position")
            .Scaling = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Scaling")
            .Shading.BackgroundPatternColor = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Shading").item("BackgroundPatternColor")
            .Shading.BackgroundPatternColorIndex = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Shading").item("BackgroundPatternColorIndex")
            .Shading.ForegroundPatternColor = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Shading").item("ForegroundPatternColor")
            .Shading.ForegroundPatternColorIndex = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Shading").item("ForegroundPatternColorIndex")
            .Shading.Texture = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Shading").item("Texture")
            .Shadow = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Shadow")
            .Size = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Size")
            .SizeBi = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("SizeBi")
            .SmallCaps = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("SmallCaps")
            .Spacing = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Spacing")
            .StrikeThrough = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("StrikeThrough")
            .Subscript = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Subscript")
            .Underline = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("Underline")
            .UnderlineColor = Globals.Ribbons.Ribbon1.obj.item(type).item("字体").item("UnderlineColor")
        End With
        oWord.Selection.Font = myFontFormat

    End Sub
    Private Sub 公文标题_Click_1(sender As Object, e As RibbonControlEventArgs) Handles 公文标题.Click
        设置格式（"公文标题"）
    End Sub

    Private Sub 二级标题_Click_1(sender As Object, e As RibbonControlEventArgs) Handles 二级标题.Click
        设置格式（“二级标题”）
    End Sub

    Private Sub 三级标题_Click_1(sender As Object, e As RibbonControlEventArgs) Handles 三级标题.Click
        设置格式（“三级标题”）
    End Sub

    Private Sub 正文_Click(sender As Object, e As RibbonControlEventArgs) Handles 正文.Click
        设置格式（“正文”）
    End Sub
    Private Sub 行间距小_Click_1(sender As Object, e As RibbonControlEventArgs) Handles 小行间距.Click
        With oWord.Selection.ParagraphFormat
            .LineSpacingRule = obj.item("小行间距"）.item("LineSpacingRule")
            .LineSpacing = obj.item("小行间距"）.item("LineSpacing")
            .LineUnitAfter = obj.item("小行间距"）.item("LineUnitAfter")
            .LineUnitBefore = obj.item("小行间距"）.item("LineUnitBefore")
            .CharacterUnitLeftIndent = obj.item("小行间距"）.item("CharacterUnitLeftIndent")
            .CharacterUnitRightIndent = obj.item("小行间距"）.item("CharacterUnitRightIndent")
            .LeftIndent = obj.item("小行间距"）.item("LeftIndent")
            .RightIndent = obj.item("小行间距"）.item("RightIndent")
            .SpaceAfter = obj.item("小行间距"）.item("SpaceAfter")
            .SpaceBefore = obj.item("小行间距"）.item("SpaceBefore")
        End With
    End Sub

    Private Sub 行间距中_Click(sender As Object, e As RibbonControlEventArgs) Handles 中行间距.Click
        With oWord.Selection.ParagraphFormat
            .LineSpacingRule = obj.item("中行间距"）.item("LineSpacingRule")
            .LineSpacing = obj.item("中行间距"）.item("LineSpacing")
            .LineUnitAfter = obj.item("中行间距"）.item("LineUnitAfter")
            .LineUnitBefore = obj.item("中行间距"）.item("LineUnitBefore")
            .CharacterUnitLeftIndent = obj.item("中行间距"）.item("CharacterUnitLeftIndent")
            .CharacterUnitRightIndent = obj.item("中行间距"）.item("CharacterUnitRightIndent")
            .LeftIndent = obj.item("中行间距"）.item("LeftIndent")
            .RightIndent = obj.item("中行间距"）.item("RightIndent")
            .SpaceAfter = obj.item("中行间距"）.item("SpaceAfter")
            .SpaceBefore = obj.item("中行间距"）.item("SpaceBefore")
        End With
    End Sub

    Private Sub 行间距大_Click(sender As Object, e As RibbonControlEventArgs) Handles 大行间距.Click
        With oWord.Selection.ParagraphFormat
            .LineSpacingRule = obj.item("大行间距"）.item("LineSpacingRule")
            .LineSpacing = obj.item("大行间距"）.item("LineSpacing")
            .LineUnitAfter = obj.item("大行间距"）.item("LineUnitAfter")
            .LineUnitBefore = obj.item("大行间距"）.item("LineUnitBefore")
            .CharacterUnitLeftIndent = obj.item("大行间距"）.item("CharacterUnitLeftIndent")
            .CharacterUnitRightIndent = obj.item("大行间距"）.item("CharacterUnitRightIndent")
            .LeftIndent = obj.item("大行间距"）.item("LeftIndent")
            .RightIndent = obj.item("大行间距"）.item("RightIndent")
            .SpaceAfter = obj.item("大行间距"）.item("SpaceAfter")
            .SpaceBefore = obj.item("大行间距"）.item("SpaceBefore")
        End With
    End Sub

    Private Sub 单个发文_Click(sender As Object, e As RibbonControlEventArgs) Handles 单个发文.Click
        Dim objTemplate As Template
        Dim objBB As BuildingBlock
        objTemplate = oWord.Templates(1)
        objBB = objTemplate.BuildingBlockEntries.Item("单行发文")
        objBB.Insert(oWord.Selection.Range)
    End Sub

    Private Sub 多个发文_Click(sender As Object, e As RibbonControlEventArgs) Handles 多个发文.Click
        Dim objTemplate As Template
        Dim objBB As BuildingBlock
        objTemplate = oWord.Templates(1)
        objBB = objTemplate.BuildingBlockEntries.Item("联合发文")
        objBB.Insert(oWord.Selection.Range)
    End Sub

    Private Sub 红色便签头_Click(sender As Object, e As RibbonControlEventArgs) Handles 红色便签头.Click
        Dim objTemplate As Template
        Dim objBB As BuildingBlock
        objTemplate = oWord.Templates(1)
        objBB = objTemplate.BuildingBlockEntries.Item("红色便签头")
        objBB.Insert(oWord.Selection.Range)
    End Sub
    Private Sub 间隔线_Click(sender As Object, e As RibbonControlEventArgs) Handles 间隔线.Click
        Dim objTemplate As Template
        Dim objBB As BuildingBlock
        objTemplate = oWord.Templates(1)
        objBB = objTemplate.BuildingBlockEntries.Item("间隔线")
        objBB.Insert(oWord.Selection.Range)
    End Sub
    Private Sub 单行版纪_Click_1(sender As Object, e As RibbonControlEventArgs) Handles 单行版纪.Click
        Dim objTemplate As Template
        Dim objBB As BuildingBlock
        objTemplate = oWord.Templates(1)
        objBB = objTemplate.BuildingBlockEntries.Item("单行版纪")
        objBB.Insert(oWord.Selection.Range)
    End Sub

    Private Sub 多行版纪_Click_1(sender As Object, e As RibbonControlEventArgs) Handles 多行版纪.Click
        Dim objTemplate As Template
        Dim objBB As BuildingBlock
        objTemplate = oWord.Templates(1)
        objBB = objTemplate.BuildingBlockEntries.Item("多行版纪")
        objBB.Insert(oWord.Selection.Range)
    End Sub

    Private Sub 页面设置_Click(sender As Object, e As RibbonControlEventArgs) Handles 页面设置.Click
        With oWord.ActiveDocument.Range.PageSetup
            .TopMargin = Globals.Ribbons.Ribbon1.obj.item("页面设置").item("TopMargin")
            .BottomMargin = Globals.Ribbons.Ribbon1.obj.item("页面设置").item("BottomMargin")
            .LeftMargin = Globals.Ribbons.Ribbon1.obj.item("页面设置").item("LeftMargin")
            .RightMargin = Globals.Ribbons.Ribbon1.obj.item("页面设置").item("RightMargin")
        End With
    End Sub



    Private Sub 便签头空行_Click(sender As Object, e As RibbonControlEventArgs) Handles 便签头空行.Click
        Dim objTemplate As Template
        Dim objBB As BuildingBlock
        objTemplate = oWord.Templates(1)
        objBB = objTemplate.BuildingBlockEntries.Item("便签头空行")
        objBB.Insert(oWord.Selection.Range)
    End Sub
    Private Sub 去除首尾空格_Click(sender As Object, e As RibbonControlEventArgs) Handles 去除首尾空格.Click
        oWord.Application.ScreenUpdating = False
        oWord.ActiveDocument.Range.Find.Execute("^l",,,,,,,,, "^p", WdReplace.wdReplaceAll)
        Dim paraLength = oWord.ActiveDocument.Paragraphs.Count
        Dim i As Paragraph, n As Long
        For Each i In oWord.ActiveDocument.Paragraphs

            For n = 1 To i.Range.Characters.Count
                If (i.Range.Text Like " *" Or i.Range.Text Like "　*" Or i.Range.Text Like Chr(9) & "*") Then
                    i.Range.Characters(1).Delete()
                ElseIf i.Range.Text Like "* " & Chr(13) Or i.Range.Text Like "* " & Chr(12) Or i.Range.Text Like "*　" & Chr(12) Or i.Range.Text Like "*　" & Chr(13) Or i.Range.Text Like "*" & Chr(9) & Chr(13) Then
                    Do
                        i.Range.Select()
                        oWord.Selection.EndKey(WdUnits.wdLine)
                        oWord.Selection.TypeBackspace()
                    Loop Until Asc(i.Range.Characters(i.Range.Characters.Count - 1).Text) <> 32 Or Asc(i.Range.Characters(i.Range.Characters.Count - 1).Text) <> 9
                ElseIf (i.Range.Find.Execute(" ^m",,,,,,,,, "^m", WdReplace.wdReplaceAll)) Then
                ElseIf (i.Range.Find.Execute("　^m",,,,,,,,, "^m", WdReplace.wdReplaceAll)) Then
                ElseIf (i.Range.Find.Execute("^m ",,,,,,,,, "^m", WdReplace.wdReplaceAll)) Then
                ElseIf (i.Range.Find.Execute("^m　",,,,,,,,, "^m", WdReplace.wdReplaceAll)) Then
                Else
                    Exit For
                End If
            Next n
            ' If Len(i.Range) = 1 Then i.Range.Delete
        Next
        oWord.Application.ScreenUpdating = True
    End Sub

    Private Sub 自动编号转文本_Click(sender As Object, e As RibbonControlEventArgs) Handles 自动编号转文本.Click
        oWord.ActiveDocument.Content.ListFormat.ConvertNumbersToText()
    End Sub
End Class
